//
//  JourneyViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyViewController.h"
#import "JourneyCollectionViewCell.h"
#import "JourneyDataSource.h"

#import "JourneryNetHandler.h"

#import "JourneyHeaderView.h"
#import "JourneyHeaderViewController.h"

#import "JourneyDetilDataSource.h"
#import "JourneyDetilViewController.h"

#import "JourneyCoutry_CityDetilViewController.h"
#import "JourneyCoutry_CityDataSource.h"

#import "MBProgressHUD.h"

@interface JourneyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,MBProgressHUDDelegate>

{
    long long expectedLength;
    long long currentLength;
}
@property (nonatomic ,retain)JourneyDataSource *journeyData;
@property (nonatomic , retain)UICollectionView *collectionView;
@property (nonatomic , retain)NSMutableArray *journeyArr;
@property (nonatomic , retain)NSMutableArray *journeyDetilArr;
@property (nonatomic , retain)JourneyHeaderView *journeyHeaderView;

@property (nonatomic ,retain)MBProgressHUD *progress;
@end

@implementation JourneyViewController
-(void)dealloc
{
    [_collectionView release];
    [_journeyArr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    //    [super viewWillAppear:animated];
    //    self.navigationController.navigationBar.translucent = YES;
    //    [[UINavigationBar appearance] setTintColor:[UIColor colorWithWhite:0.0 alpha:0.3]];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark 是否忽略navigationBar的坐标
    //是否忽略navigationBar的坐标
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(140, 130);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    //边缘距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 15);
    //设置头部并给定大小
    [flowLayout setHeaderReferenceSize:CGSizeMake(_collectionView.frame.size.width, 35)];
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 105) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.userInteractionEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    
    
    
#pragma mark 注册collecttionView
    //注册collecttionView
    [_collectionView registerClass:[JourneyCollectionViewCell class] forCellWithReuseIdentifier:@"旅行cell"];
    
#pragma mark 注册头部视图
    
    //注册头部视图
    [_collectionView registerClass:[JourneyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    //添加视图
    [self.view addSubview:_collectionView];
    
    //添加菊花的时候要注意是否被别的视图盖上
    
    self.progress = [[MBProgressHUD showHUDAddedTo:self.view animated:YES]retain];
    [_progress show:YES];
    self.progress.delegate = self;
    
    //网络请求
    [self netHandler];
    
    
    //内存管理
    [_collectionView release];
    [flowLayout release];
}

#pragma mark 设置头部视图

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    self.journeyHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    
#pragma mark 判断button
    
    JourneyDataSource *jdsModel = [_journeyArr objectAtIndex:indexPath.section];
    _journeyHeaderView.index = jdsModel.index;
    //    NSLog(@"%ld",jdsModel.index);
    
    _journeyHeaderView.headerLable.text = jdsModel.title;
    
    reusableView = _journeyHeaderView;
    //    if (indexPath.section == 3 ||
    //        indexPath.section == 5 ||
    //        indexPath.section == 6 ||
    //        indexPath.section == 7 ) {
    //        _journeyHeaderView.headerBtu.hidden = NO;
    //    }
    //    if (jdsModel.index == 4 ||
    //        jdsModel.index == 6 ||
    //        jdsModel.index == 7 ||
    //        jdsModel.index == 8 ) {
    //        _journeyHeaderView.headerBtu.hidden = NO;
    //    }
    
#pragma mark 通过block push 新的页面
    [_journeyHeaderView setBlock:^{
        JourneyHeaderViewController *detilHearderVC = [[JourneyHeaderViewController alloc]init];
        detilHearderVC.index = jdsModel.index;
        //        NSLog(@"%ld",jdsModel.index);
        [self.navigationController pushViewController:detilHearderVC animated:NO];
        [detilHearderVC release];
    }];
    
    return  reusableView;
}

//collectionView的section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.journeyArr count];
}

//collectionView 每个section cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    JourneyDataSource *jdsModel = [self.journeyArr objectAtIndex:section];
    return [jdsModel.data count];
}

#pragma mark cell 赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    JourneyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"旅行cell" forIndexPath:indexPath];
    
    JourneyDataSource * jdsModel = _journeyArr[indexPath.section];
    JourneyDetilDataSource * data = jdsModel.data[indexPath.row];
    
    cell.jourDetilData = data;
    
    //    NSLog(@"indexPath = %@, data.index = %ld",indexPath,jdsModel.index);
    return cell;
    
    
}
#pragma mark CELL 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 || indexPath.section == 6) {
        JourneyDetilViewController *joudetil = [[JourneyDetilViewController alloc]init];
        
        JourneyDataSource * jdsModel = _journeyArr[indexPath.section];
        joudetil.journeyData = jdsModel.data[indexPath.item];
        joudetil.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:joudetil animated:NO];
        joudetil.hidesBottomBarWhenPushed = NO;
        [joudetil release];
        
    }else
    {
        JourneyCoutry_CityDetilViewController *jouCityDetilVC = [[JourneyCoutry_CityDetilViewController alloc]init];
        JourneyDataSource * jdsModel = _journeyArr[indexPath.section];
        jouCityDetilVC.coutry_CityData =jdsModel.data[indexPath.item];
        [jouCityDetilVC setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:jouCityDetilVC animated:NO];
        [jouCityDetilVC setHidesBottomBarWhenPushed:NO];
        [jouCityDetilVC release];
    }
    
}

#pragma mark

- (void)netHandler
{
    
    
    NSString *str = @"http://api.breadtrip.com/destination/v3/";
    self.journeyArr = [NSMutableArray array];
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        
        NSMutableArray *Arr = [data objectForKey:@"elements"];
        for (NSMutableDictionary *dic in Arr) {
            
            JourneyDataSource *jdsModel = [[JourneyDataSource alloc] initWithDictionary:dic];
            
            NSMutableArray * dataArray = [dic objectForKey:@"data"];
            
            NSMutableArray *secArr = [NSMutableArray array];
            
            for (NSMutableDictionary *dic in dataArray) {
                JourneyDetilDataSource *data = [[JourneyDetilDataSource alloc]initWithDictionary:dic];
                [secArr addObject:data];
                [data release];
            }
            
            jdsModel.data = secArr;
            
            [self.journeyArr addObject:jdsModel];
            [jdsModel release];
        }
        
        [_collectionView reloadData];
        [_progress hide:YES];
    }];
    
    
    
}



#pragma mark - Hide MB


- (void)hudWasHidden:(MBProgressHUD *)hud
{
    
    [_progress removeFromSuperview];
    [_progress release];
    _progress  = nil;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
